#include "RttyProcessManager.h"

#include <wait.h>

#include "ConfigManager.h"

RttyProcessManager::RttyProcessManager() {
    //ctor
}

RttyProcessManager::~RttyProcessManager() {
    //dtor
}


void RttyProcessManager::killRuning(void){
    const char* argument[]{nullptr, "rtty", nullptr};

    mprocessCreator.runProcess("/usr/bin/killall", "killall", argument);

}


std::string& RttyProcessManager::buildArgument(std::string& output){


    return output;
}


//root/tools/rtty -I $DEVICE_ID -h $HOST_IP -p $PORT -a -v -d $DESC &
bool RttyProcessManager::runRtty(void){
    ConfigManager& configManager = ConfigManagerSingleton::instance();

    ConfigInfo& config = configManager.getConfigInfo();
    VersionInfo& versionInfo = configManager.getVersionInfo();
    const char* ip = config.serverAddress.ip.data();
//    const char* ip = DEFAULT_SERVER_ADDRESS == config.serverAddress.ip ? "182.254.241.137" : config.serverAddress.ip.data();

    const char* argument[]{nullptr, "-a", "-v", "-I", versionInfo.uuidString.data(), "-h", ip, "-p", "5912", "-d", versionInfo.firmwareVersionString.data(), nullptr};

    __pid_t pid{0};
    mprocessCreator.createBackgroundProcess("/root/tools/rtty", "rtty", argument);
    /*
    pid = fork();
    int code{0};
    if (0 == pid){
        setsid();

        ::exit(0);
    } else {
        pid = ::waitpid(pid, &code, WUNTRACED);
    }*/
    return true;
}


